
/*******************************************************************************
 * Copyright (c) 2007 Jules White. All rights reserved.
 * This program and the accompanying materials are made available under the
 * terms of the Common Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/cpl-v10.html
 * 
 * Contributors: Jules White - initial API and implementation
 ******************************************************************************/
package org.refresh.prolog;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.StringTokenizer;

import com.sun.xml.internal.bind.v2.runtime.property.Property;

public class BasicPrologConverter implements PrologConverter {

	public List listToObjects(ObjectManager mgr, String val) {
		StringTokenizer tk = new StringTokenizer(val, "[],");
		List result = new ArrayList();
		while (tk.hasMoreTokens()) {
			String id = tk.nextToken();
			Object child = mgr.getObject(id);

			result.add(child);
		}
		return result;
	}
	
	public List listToValues(ObjectManager mgr, String val) {
		val = val.trim();
		val = val.substring(1,val.length()-1);
		StringTokenizer tk = new StringTokenizer(val, "[],",true);
		List result = new LinkedList();
		while (tk.hasMoreTokens()) {
			String v = tk.nextToken();
			Object child = null;
			if(v.equals(","))
				continue;
			else if(v.equals("["))
				child = getList(mgr,tk);
			else
				child = toObject(mgr, v);
			result.add(child);
		}
		return result;
	}
	
	public List getList(ObjectManager mgr, StringTokenizer tk){
		LinkedList vals = new LinkedList();
		while (tk.hasMoreTokens()) {
			String v = tk.nextToken();
			Object child = null;
			if(v.equals("]"))
				break;
			
			if(v.equals("[")){
				child = getList(mgr,tk);
			}
			else if(v.equals(","))
				continue;
			else{
			    child = toObject( mgr, v);
			}
			vals.add(child);
		}
		return vals;
	}
	
	

	public Object toObject(ObjectManager om, String result) {
		return toObject(om, (Object)result);
	}

	public Object toObject(ObjectManager mgr,
			Object val) {
		if (val instanceof String && ((String) val).startsWith("[")) {
			return listToValues(mgr,(String)val);
		} else if (val instanceof String && ((String) val).trim().startsWith("'")) {
			String sval = (String)val;
			int start = sval.indexOf("'")+1;
			int end = sval.lastIndexOf("'");
			sval = sval.substring(start,end);
			return sval;
		} else if (val instanceof String){
			String sval = (String)val;
			
			if(sval.startsWith("id_")){
				return mgr.getObject((String) val);
			}
			try{
				int ival = Integer.parseInt(sval);
				return ival;
			}catch (Exception e) {}
			
			try{
				double dval = Double.parseDouble(sval);
				return dval;
			}catch (Exception e) {}
			
			try{
				boolean tval = Boolean.parseBoolean(sval);
				return tval;
			}catch (Exception e) {}
		}
		return val;
	}

	

//	public MetaType findType(MetaInformation mi, String typename) {
//		List<MetaType> types = mi.getTypes();
//		for (MetaType type : types) {
//			if (type.getName().toLowerCase().equals(typename))
//				return (MetaType) type;
//		}
//		return null;
//	}

	public List<String> stringToList(StringTokenizer str) {
		LinkedList<String> list = new LinkedList<String>();
		String token = str.nextToken();
		while (!"]".equals(token) && !")".equals(token)) {
			list.add(token);
			if (!str.hasMoreTokens())
				break;
			token = str.nextToken();
		}
		return list;
	}
	
}
